﻿using Sword.Data;
using Sword.ProxyCore;
using Sword.Utility;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sword.WorkBench.Properties;

namespace Sword.WorkBench
{
    public partial class MainForm : Form
    {
        AppConfigRootEntity appConfig = null;

        public CoreService coreService = null;


        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {

            if (this.DesignMode)
            {
                return;
            }
            this.Text = "前端开发之利器(" + Resources.version + ")";
            appConfig = XmlUtility.LoadConfigs();

            coreService = new CoreService(appConfig)
            {
                CallbackUpdateMessage = UpdateMessage,
                CallbackWarning = WarningMessage,
                StartCompleted = StartCompletedCallback,
                StopCompleted = StopCompletedCallback,
                RulesLoadCompleted = RulesLoadCompleted
            };
            

            coreService.Start();

        }

        void UpdateMessage(string message)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action(delegate()
                {
                    this.Text = string.Format("[{0}] {1}", DateTime.Now.ToString("MM-dd HH:mm:ss.fff"), message);
                }));
            }
            else
            {
                this.Text = string.Format("[{0}] {1}", DateTime.Now.ToString("MM-dd HH:mm:ss.fff"), message);
            }
        }

        void WarningMessage(string message)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action(delegate()
                {
                    this.Text = string.Format("[{0}] {1}", DateTime.Now.ToString("MM-dd HH:mm:ss.fff"), message);
                }));
            }
            else
            {
                this.Text = string.Format("[{0}] {1}", DateTime.Now.ToString("MM-dd HH:mm:ss.fff"), message);
            }
            
        }

        void RulesLoadCompleted(List<ConditionItem> rules)
        {
            //this.rules.Clear();
            //this.rules.AddRange(rules);
        }
        void StartCompletedCallback()
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new Action(() =>
                {
                }));
            }
            else
            {
            }
        }

        void StopCompletedCallback()
        {
            if (this.InvokeRequired)
            {
                this.BeginInvoke(new Action(() =>
                {
                }));
            }
            else
            {
            }
        }

        private void MainForm_Resize(object sender, EventArgs e)
        {
        }


        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (coreService != null)
            {
                coreService.ShutDown();
            }
        }

    }
}
